package com.yudaxian.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yudaxian.common.vo.Result;
import com.yudaxian.sys.entity.Logs;
import com.yudaxian.sys.service.ILogsService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统日志相关接口
 */
@RestController
@RequestMapping("/logs")
public class LogsController {


    @Resource
    private ILogsService LogsService;

    @GetMapping("/all")
    public Result<List<Logs>> getAllLogs() {
        List<Logs> logs = LogsService.list();
        return Result.success(logs);
    }

    @GetMapping("/list")
    public Result<Map<String, Object>> getLogsList(
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "pageNo") Integer pageNo,
            @RequestParam(value = "pageSize") Integer pageSize) {

        LambdaQueryWrapper<Logs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(username), Logs::getUsername, username);
        wrapper.orderByDesc(Logs::getId);

        Page<Logs> page = new Page<>(pageNo, pageSize);
        LogsService.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("totalPage", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }

    @GetMapping("{id}")
    public Result<Logs> getLogsById(@PathVariable("id") Integer id) {
        Logs logs = LogsService.getLogsById(id);

        return Result.success(logs);
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> deleteLogsById(@PathVariable("id") Integer id) {
        LogsService.deleteLogsById(id);

        return Result.success();
    }


}
